package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.service.AdminService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lixu
 */
@RequestMapping("/admin/admin")
@RestController
@CrossOrigin
public class AdminController {


    @Autowired
    private AdminService adminService;

    /**
     * http://localhost:8080/admin/admin
     *    /list?page=1&limit=20&username=qianfeng&sort=add_time&order=asc
     *
     *    - 接口地址 : http://localhost:8083/admin/admin/list?page=1&limit=20&sort=add_time&order=desc
     * - GET请求
     * - 下行数据 :
     *  GET /admin/admin/list
     */
    @RequiresPermissions(value = "admin:admin:list")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "查询")//通过这个注解知道这是什么方法是干什么的？
    @GetMapping("/list")
    public Object list(String username, String sort, String order,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit){
        //分页对象
        IPage<DtsAdmin> iPage = adminService.findListByQuery(username,sort,order,page,limit);
        Map data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * /create
     */
    @RequiresPermissions(value = "admin:admin:create")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsAdmin dtsAdmin){

        dtsAdmin = adminService.create(dtsAdmin);
        return ResponseUtil.ok(dtsAdmin);
    }




}
